home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3096 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.5 KB  |  85 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!cman
  3. From: cman@netcom.com (Mike Austin)
  4. Subject: Re: [Q] Best way to "define" flag values?
  5. Message-ID: <cmanDLKAqG.Drt@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <marnoldDLIv9w.1s4@netcom.com>
  9. Distribution: na
  10. Date: Mon, 22 Jan 1996 02:54:16 GMT
  11. Sender: cman@netcom.netcom.com
  12.  
  13. Here's a little essay that I did a while ago.  I still think the same 
  14. way now, but my views have broadened...
  15.  
  16. Enumerations and Parameterless Macros
  17.  
  18.     Enumerations have always been used in C and C++, but there are 
  19. different opinions on whether they should be used or not.  As 
  20. enumerations provide the ability to use symbolic constants, so does the 
  21. #define statement.  So what are the major differences of the two?  
  22. Enumerations are more like a data-type; you can nest enumerations in 
  23. classes and structures, helping in supporting encapsulation.  
  24. Enumerations are less dependent on the actual value represented; if you 
  25. insert a new enumeration constant, you don't have to declare the value 
  26. associated with it.  Because it helps support encapsulation, there are 
  27. less names used in the global name-space.
  28.  
  29.    Parameterless macros (#define XXX YYY) have been used in C since it's 
  30. inception, and has made its way to C++ code although mostly used for 
  31. pseudo-templates and other hacky uses.  Still, if you would remove 
  32. support for these, many C++ programs would break.  The obvious breakage 
  33. would be because of C++'s lack of a smart include file handling.  Not 
  34. only is it tedious and takes up space to write:
  35.  
  36. #ifndef FILE_H
  37. #define FILE_H
  38. ...
  39. #endif
  40.  
  41. but the file must be opened each time, and read to the end of the file. 
  42. Not to get off track, but it is also inefficient in that it has to 
  43. compile the included file every time.  Enumerations are clearly the ones 
  44. to use in C++ code, but in my eyes, they could be improved.  First off, 
  45. #defines have been used for defining bit sets for a long time.  
  46. Enumerations can also handle the task, but you must specify the values to 
  47. get the desired result.  For example:
  48.  
  49. enum Style { NoTitle = 0x0001, NoBorder = 0x0002, NoResize = 0x0004};
  50.  
  51. must be spelled out explicitly to work correctly.  I find it would make 
  52. programming much easier if there was a keyword 'enumbit' which performed 
  53. the above with the more simple:
  54.  
  55. enum Style { NoTitle, NoBorder, NoResize };
  56.  
  57. not only is it easier to read, it avoids mistakes like creating 
  58. duplicates, misplacements, etc.  I see one other improvement that might 
  59. not gain much popularity, but reduce source code size and redundancy.  
  60. Say you have class Window which contains the enumeration described above.
  61.  
  62. class Window {
  63.  public:
  64.     Window(Style aStyle);
  65.     enumbit Style { NoTitle, NoBorder, NoResize };
  66. };
  67.  
  68. create a window with all three window styles set:
  69.  
  70.     win(Window::NoTitle | Window::NoBorder | Window::NoResize);
  71.  
  72. not very elegant.  Why would you have to use scoping when the compiler 
  73. knows the argument must be of type Style?  Some people may say it would 
  74. degrade the consistency of c++'s scope mechanism, but I feel it 
  75. over-weighs the argument.  Some people also say that they don't program 
  76. in this fashion, and they don't use enumerations or macros at all.
  77.  
  78. -- 
  79.  ------------------------------------------------------------------------------
  80.  
  81.             "I just got paid today, got me a pocket full of change"
  82.  
  83.    <A HREF="ftp://ftp.netcom.com/pub/cm/cman/index.html">Mike's Home Page</A>
  84.          Links to FAQs; cool commercial, graphics and music pages, etc.
  85.